﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VectorDraw.Professional.vdObjects;
using VectorDraw.Professional.vdFigures;

namespace PDNPAS
{
    public partial class frmSubstation : DevExpress.XtraEditors.XtraForm
    {
        public frmSubstation()
        {
            InitializeComponent();
        }

        private void frmSubstation_Load(object sender, EventArgs e)
        {
            int substIndex = StaticVariables.substationIndex;
            if (substIndex > -1)
            {
                txtSubstName.Text = StaticVariables.substationList[substIndex].SubstName;
                //  public Substation(string substNameP, double substXP,double substYP,
                //int iVoltageLevelP,int oVoltageLevelP,vdBlock blkSubstationP)
                txtSubstType.Text = StaticVariables.substationList[substIndex].SubstType;
                txtSubstX.Text = StaticVariables.substationList[substIndex].InsSubstation.InsertionPoint.x.ToString("0.00");
                txtSubstY.Text = StaticVariables.substationList[substIndex].InsSubstation.InsertionPoint.y.ToString("0.00");
                //MessageBox.Show("substationList" + StaticVariables.substationList[substIndex].SubstX.ToString("0.00") + "，" + StaticVariables.substationList[substIndex].SubstY.ToString("0,00"));
                txtLoad.Text = StaticVariables.substationList[substIndex].Load.ToString("0.00");
                txtInputLvl.Text = StaticVariables.substationList[substIndex].IVoltageLevel.ToString();
                txtOutputLvl.Text = StaticVariables.substationList[substIndex].OVoltageLevel.ToString();
                txtCapacity.Text = StaticVariables.substationList[substIndex].Capacity.ToString();
                txtTransfNum.Text = StaticVariables.substationList[substIndex].TransfNum.ToString();
                txtIntervalNum.Text = StaticVariables.substationList[substIndex].IntervalNum.ToString();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            int substIndex = StaticVariables.substationIndex;
            if (substIndex > -1)
            {
                txtSubstName.Text = StaticVariables.substationList[substIndex].SubstName;
                //  public Substation(string substNameP, double substXP,double substYP,
                //int iVoltageLevelP,int oVoltageLevelP,vdBlock blkSubstationP)
                txtSubstType.Text = StaticVariables.substationList[substIndex].SubstType;
                txtSubstX.Text = StaticVariables.substationList[substIndex].SubstX.ToString("0.00");
                txtSubstY.Text = StaticVariables.substationList[substIndex].SubstY.ToString("0.00");
                //MessageBox.Show("substationList" + StaticVariables.substationList[substIndex].SubstX.ToString("0.00") + "，" + StaticVariables.substationList[substIndex].SubstY.ToString("0,00"));
                txtInputLvl.Text = StaticVariables.substationList[substIndex].IVoltageLevel.ToString();
                txtOutputLvl.Text = StaticVariables.substationList[substIndex].OVoltageLevel.ToString();
                txtCapacity.Text = StaticVariables.substationList[substIndex].Capacity.ToString();
                txtTransfNum.Text = StaticVariables.substationList[substIndex].TransfNum.ToString() ;
                txtIntervalNum.Text = StaticVariables.substationList[substIndex].IntervalNum.ToString();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int substIndex = StaticVariables.substationIndex;
            if (txtSubstName.Text == "" || txtSubstType.Text == "" || txtInputLvl.Text == "" ||
                txtOutputLvl.Text == "" || txtSubstX.Text == "" || txtSubstY.Text == "" || txtTransfNum.Text == "" ||
                txtCapacity.Text == "" || txtIntervalNum.Text == "")
            {
                MessageBox.Show("请将所有属性填写完整！");
                return;
            }

            if (substIndex > -1)
            {
                StaticVariables.substationList[substIndex].SubstName = txtSubstName.Text;
                StaticVariables.substationList[substIndex].SubstType = txtSubstType.Text;
                StaticVariables.substationList[substIndex].IVoltageLevel = int.Parse(txtInputLvl.Text);
                StaticVariables.substationList[substIndex].OVoltageLevel = int.Parse(txtOutputLvl.Text);
                StaticVariables.substationList[substIndex].SubstX = double.Parse(txtSubstX.Text);
                StaticVariables.substationList[substIndex].SubstY = double.Parse(txtSubstY.Text);
                StaticVariables.substationList[substIndex].TransfNum = int.Parse(txtTransfNum.Text);
                StaticVariables.substationList[substIndex].Capacity = int.Parse(txtCapacity.Text);
                StaticVariables.substationList[substIndex].IntervalNum = int.Parse(txtIntervalNum.Text);


                //vdXProperty prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("变电站名称");
                //prop.PropValue = txtSubstName.Text;
                //prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("变电站型号");
                //prop.PropValue = txtSubstName.Text;
                //prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("输入电压等级");
                //prop.PropValue = int.Parse(txtInputLvl.Text);
                //prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("输出电压等级");
                //prop.PropValue = int.Parse(txtOutputLvl.Text);
                //prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("主变数量");
                //prop.PropValue = int.Parse(txtTransfNum.Text);
                //prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("主变容量");
                //prop.PropValue = int.Parse(txtCapacity.Text);
                //prop = StaticVariables.substationList[substIndex].InsSubstation.Block.XProperties.FindName("出线间隔");
                //prop.PropValue = int.Parse(txtIntervalNum.Text);

                StaticVariables.substationList[substIndex].InsSubstation.Attributes[0].ValueString = txtSubstName.Text;
                StaticVariables.substationList[substIndex].InsSubstation.InsertionPoint.x = double.Parse(txtSubstX.Text);
                StaticVariables.substationList[substIndex].InsSubstation.InsertionPoint.y = double.Parse(txtSubstY.Text);
                StaticVariables.substationList[substIndex].InsSubstation.Attributes[0].InsertionPoint.x = StaticVariables.substationList[substIndex].InsSubstation.InsertionPoint.x;
                StaticVariables.substationList[substIndex].InsSubstation.Attributes[0].InsertionPoint.y = StaticVariables.substationList[substIndex].InsSubstation.InsertionPoint.y + 1.6 * StaticVariables.substationList[substIndex].InsSubstation.Yscale;
                StaticVariables.substationList[substIndex].InsSubstation.Update();
                StaticVariables.substationList[substIndex].InsSubstation.Invalidate();
                StaticVariables.vdFC.BaseControl.ActiveDocument.Redraw(true);
                
            }
            StaticVariables.substationIndex = -1;
            this.Close();
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int substIndex = StaticVariables.substationIndex;
            if (substIndex > -1)
            {
                vdInsert ins = new vdInsert();
                ins = StaticVariables.substationList[substIndex].InsSubstation;
                //ins.ShowGrips = false;
                //StaticVariables.vdFC.BaseControl.ActiveDocument.Model.Entities.RemoveItem(ins);    
                ins.Deleted = true;
                ins.Update();
                ins.Invalidate();
                StaticVariables.vdFC.BaseControl.ActiveDocument.Redraw(true);
                StaticVariables.substationList.RemoveAt(substIndex);
            }
            StaticVariables.substationIndex = -1;
            this.Close();
        }


    }
}